{extend name="public/base"/}
{block name="style"}
{/block}
{block name="content"}
<div class="data-list" data-url="{:url('allot')}">
    <form class="layui-form inline-form">
        <div class="pull-left">
            <div class="layui-inline">
                <input type="text" id="allot-sn" autofocus="autofocus" placeholder="调拨单号/追踪号" class="layui-input input-order-sn">
            </div>
            <div class="layui-inline">
                <select name="out_id" lay-filter="data-list" lay-search>
                    <option value="">调出仓库</option>
                    {foreach $warehouse as $w}
                    <option value="{$w.warehouse_id}">{$w.name}</option>
                    {/foreach}
                </select>
            </div>
            <div class="layui-inline">
                <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" id="allot-recv-search"><i class="layui-icon layui-icon-search"></i></button>
            </div>
        </div>
    </form>
    <div class="layui-row form-lists-button">
        <div class="layui-inline layui-btn-group">
            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="allot-batch-recv"><i class="layui-icon layui-icon-cart"></i>批量收货</button>
        </div>
    </div>
    <div class="data">
        <p><i class="fa fa-spinner fa-spin"></i> 加载中...</p>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
layui.use(['layer', 'jquery', 'form', 'longli'], function()
{
    var layer = layui.layer
        , $ = layui.jquery
        , form = layui.form;
    // 收货
    $("#allot-recv-search").click(function()
    {
        var $input = $("#allot-sn")
            , text = $.trim($input.val())
            , type = $("#search-type").val();
        if(!text) return false;
        var url = "{:url('allot_recv')}?sn=" + text + "&type=" + type;
        $.get(url).done(function(html)
        {
            if($.type(html) == "object")
            {
                if(html.data.type) layui.longli.playMP3(html.data.type);
                layer.msg(html.msg, {icon: 5, time: 1500});
                return false;
            }
            parent.layer.open({
                type: 1,
                title: "调拨收货",
                content: html,
                scrollbar: false,
                area: ["80%", "80%"],
                btn: ['确定', '取消'],
                yes: function (index, layero)
                {
                    var $form = $("#allot-recv");
                    $.post($form.attr('action'), $form.serialize(), function (response)
                    {
                        if(response.data.type) layui.longli.playMP3(response.data.type);
                        var icon = 5;
                        if(response.code == 1)
                        {
                            $(".data").closest('.data-list').getList();
                            icon = 6;
                            layer.close(index);
                        }
                        layer.msg(response.msg, {icon: icon, time: 1500});
                    });
                },
                end: function()
                {
                    $input.focus();
                }
            });
        });
    });

    $("body").on("click", ".allot-recv", function()
    {
        var $this = $(this)
            , item = $this.attr('item')
            , $input = $("#allot-sn");
        if(!item) return false;
        $input.val(item);
        $("#allot-recv-search").click();
        $input.val('');
    });

    // 批量收货
    $("#allot-batch-recv").click(function()
    {
        var ids = getIds('.allot-id');
        if(ids.length < 1)
        {
            layer.msg("请选择要收货的调拨单", {icon: 5, time: 1500});
            return false;
        }
        $.post("{:url('allot_recv')}", {allot_id: ids.join(',')}).done(function(response)
        {
            if(response.data.type) layui.longli.playMP3(response.data.type);
            var icon = 5;
            if(response.code == 1)
            {
                $(".data").closest('.data-list').getList();
                icon = 6;
            }
            layer.msg(response.msg, {icon: icon, time: 1500});
        });
    });

    // 输入框
    $("#allot-sn").keydown(function(event)
    {
        if(event.keyCode == 13)
        {
            $("#allot-recv-search").click();
            $(this).val('');
        }
    });
});
</script>
{/block}